﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//new
using DHNetSDK;
using System.IO;
using System.Drawing;

namespace JKZP
{
    class DHJKCamera:JKCamera
    {
        public DHJKCamera()
        {
            m_Type = PP.JKCAMERATYPE.DAHUA;
            m_Port = 37777;
            Init();
        }
        public override void Init()
        {
            m_LoginID = 0; //大华0代表登陆失败
            m_Channel = 0; //通道从0开始
            m_PlayHandle = 0;//大华0代表预览失败
            m_CamIsOpen = false;
            m_CDMC = "车道1";
            m_CDDM = "1";
            m_SnapCount = "3";
            m_TempPicName = "";
            m_FileName1 = "";
            m_FileName2 = "";
            m_FileName3 = "";
        }

        public override void Login() 
        {
            NET_DEVICEINFO deviceInfo = new NET_DEVICEINFO();
            int error = 0;
            m_LoginID = DHClient.DHLogin(m_IP, (ushort)m_Port, m_UserName, m_PassWord, out deviceInfo, out error);
        }
        public override void StartPreView(IntPtr handle)
        {
            if ((m_LoginID > 0) && (handle != IntPtr.Zero))
            {
                m_PlayHandle = DHClient.DHRealPlay(m_LoginID, m_Channel, handle);
                if (m_PlayHandle > 0) m_CamIsOpen = true;
            }
        }
        public override void StopPreView() 
        {
            if (m_PlayHandle > 0)
            {
                DHClient.DHStopRealPlay(m_PlayHandle);
                m_PlayHandle = 0;
            }
        }
        public override void Logout()
        {
            if (m_LoginID > 0)
            {
                DHClient.DHLogout(m_LoginID);
                m_LoginID = 0;
            }
        }
        public override void Play(IntPtr handle)
        {
            Init();
            Login();
            StartPreView(handle);
        }
        public override void ShutDown()
        {
            StopPreView();
            Logout();
            Init();
        }
        public override bool CaptureOnePic(string group,string index, JKCamera m_CurrentCam)
        {
            if ((m_LoginID > 0) && (m_PlayHandle > 0))
            {
                m_TempPicName = PP.gTempFolder + group + "_" + index + ".bmp";
                return DHClient.DHCapturePicture(m_PlayHandle, m_TempPicName);
            }
            return false;
        }

        public override bool SaveOneGroup(string group) 
        {
            if (PP.gJKRecordEnable == true)
            {
                return (SaveOnePic(group,"1") && SaveOnePic(group,"2") && SaveOnePic(group,"3") && SaveJKRecord());
            }
            else
            {
                return (SaveOnePic(group,"1") && SaveOnePic(group,"2") && SaveOnePic(group,"3"));
            }
        }
        private bool SaveOnePic(string group,string index)
        {
            bool ans = false;
            string splitor = "\\";
            try
            {
                //转换bmp为jpg
                string bmpfile = PP.gTempFolder + group + "_" + index + ".bmp";
                string tempjpgfile = PP.gTempFolder + group + "_" + index + ".jpg";
                byte[] pic = null;
                if (File.Exists(bmpfile))
                {
                    pic = PP.ChangeBmpFileToJpgBytes(bmpfile);
                }
                else if(File.Exists(tempjpgfile))
                {
                    pic = File.ReadAllBytes(tempjpgfile);
                }
                byte[] picWithTail = null;

                if (pic != null)
                {
                    //计算保存路径
                    string jkPicName = MakePicName(index, PP.gJKNameTypeList, PP.gJKNameTextList);
                    string jkSubPath = "";
                    if (PP.gJKSaveBySubFolder)
                    {
                        jkSubPath = MakeSubFolder(PP.gJKFolderTypeList);
                    }
                    string jkPath = PP.gJKSavePath + jkSubPath;
                    if (!Directory.Exists(jkPath)) Directory.CreateDirectory(jkPath);

                    //准备数据
                    if (PP.gJARITailEnable == true)
                    {
                        PP.structJariPicInfo info = new PP.structJariPicInfo();
                        info.m_Info1 = "";
                        info.m_Info2 = index;
                        info.m_Info3 = "0";
                        info.m_Info4 = m_HPHM;
                        info.m_Info5 = "0";
                        info.m_Info6 = "0";
                        info.m_Info7 = "0";
                        info.m_Info8 = "0";
                        info.m_Info9 = "1";
                        info.m_Info10 = "3";
                        info.m_Info11 = "";
                        info.m_Info12 = m_RoadName;
                        info.m_Info13 = "";
                        info.m_Info14 = PP.gUser.username;
                        info.m_Info15 = "";
                        info.m_Info16 = "";
                        info.m_Info17 = m_RoadID;
                        info.m_Info18 = m_WFSJ.ToString("yyyy-MM-dd HH:mm:ss");
                        info.m_Info19 = "1";
                        info.m_Info20 = "车道1";
                        info.m_Info21 = "";
                        info.m_Info22 = "";
                        info.m_Info23 = "";
                        info.m_Info24 = "";

                        //叠加尾部信息
                        picWithTail = PP.AddTailToPic(info, pic);
                        //保存
                        File.WriteAllBytes(jkPath + splitor + jkPicName, picWithTail);
                    }
                    else
                    {
                        //保存
                        File.WriteAllBytes(jkPath + splitor + jkPicName, pic);
                    }


                    //生成3张图片名，辅助生成图片记录
                    if (index == "1")
                    {
                        m_FileName1 = jkPath + splitor + jkPicName;
                    }
                    else if (index == "2")
                    {
                        m_FileName2 = jkPath + splitor + jkPicName;
                    }
                    else if (index == "3")
                    {
                        m_FileName3 = jkPath + splitor + jkPicName;
                    }
                    else { }


                    if (File.Exists(jkPath + splitor + jkPicName))
                    {
                        ans = true;
                        File.Delete(bmpfile);
                    }
                }
            }
            catch
            { }
            return ans;
        }

        public override void Left(int speed, bool stop) 
        {
            if ((m_LoginID > 0) && (m_PlayHandle>0))
            {
                //这种方法很卡，不能用
                //DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_LEFT_CONTROL, (ushort)speed, stop);

                //这种方法不能用，没有反应
                //DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_LEFT_CONTROL, 0, (ushort)speed, stop);

                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_LEFT_CONTROL, 0, (ushort)speed,0, stop);

            }
        }
        public override void Right(int speed, bool stop) 
        {
            if ((m_LoginID > 0) && (m_PlayHandle>0))
            {
                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_RIGHT_CONTROL,0, (ushort)speed,0, stop);
            }
        }
        public override void Up(int speed, bool stop)
        {
            if ((m_LoginID > 0) && (m_PlayHandle>0))
            {
                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_UP_CONTROL,0, (ushort)speed, 0,stop);
            }
        }
        public override void Down(int speed, bool stop) 
        {
            if ((m_LoginID > 0) && (m_PlayHandle>0))
            {
                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_DOWN_CONTROL,0, (ushort)speed, 0,stop);
            }
        }
        public override void ZoomIn(int speed, bool stop)
        {
            if ((m_LoginID > 0)&&(m_PlayHandle>0))
            {
                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_ZOOM_ADD_CONTROL,0, (ushort)speed, 0,stop);
            }
        }
        public override void ZoomOut(int speed, bool stop)
        {
            if ((m_LoginID > 0) && (m_PlayHandle>0))
            {
                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_ZOOM_DEC_CONTROL,0, (ushort)speed,0, stop);
            }
        }
        public override void PTZ3D(Rectangle WinRect, Point pointStart, Point pointEnd) 
        {
            if ((m_LoginID > 0) && (m_PlayHandle > 0))
            {
                try
                {
                    Point Origin = new Point(0, 0);
                    Point SendPoint = new Point(0, 0);
                    Origin.X = (WinRect.Left + WinRect.Right) / 2;
                    Origin.Y = (WinRect.Top + WinRect.Bottom) / 2;

                    int dx = (pointStart.X + pointEnd.X) / 2;
                    int dy = (pointStart.Y + pointEnd.Y) / 2;

                    int width = WinRect.Right - WinRect.Left;
                    int height = WinRect.Bottom - WinRect.Top;

                    SendPoint.X = (dx - Origin.X) * 8192 * 2 / width;
                    SendPoint.Y = (dy - Origin.Y) * 8192 * 2 / height;

                    int width2 = pointEnd.X - pointStart.X;
                    int height2 = pointEnd.Y - pointStart.Y;
                    int multiple = 0;

                    if (height2 != 0 && width2 != 0)
                    {
                        multiple = (width * height) / (width2 * height2);
                    }

                    DHClient.DHPTZControl(m_LoginID, m_Channel, true, SendPoint.X, SendPoint.Y, multiple, false);
                }
                catch { }
            }
        }
        public override void CallPreSetPoint(int index) 
        {
            if ((m_LoginID > 0) && (m_PlayHandle>0))
            {
                DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_POINT_MOVE_CONTROL,0, (ushort)index,0, false);
            }
        }
        public override bool SavePreSetPoint(int index) 
        {
            if ((m_LoginID > 0) && (m_PlayHandle > 0))
            {
                return DHClient.DHPTZControl(m_LoginID, m_Channel, PTZ_CONTROL.PTZ_POINT_SET_CONTROL, 0, (ushort)index, 0, false);
            }
            return false;
        }
        public override DateTime GetDeviceTime()
        {
            DateTime now = DateTime.Now;
            try
            {
                if ((m_LoginID > 0) && (m_PlayHandle > 0))
                {
                    NET_TIME time = new NET_TIME();
                    bool flag = DHClient.DHGetDevConfig(m_LoginID,ref time);
                    if (flag == true)
                    {
                        now = time.ToDateTime();
                    }
                }
            }
            catch { }
            return now;
        }
        public override void CheckTime()
        {
            try
            {
                if ((m_LoginID > 0) && (m_PlayHandle > 0))
                {
                    DateTime current = DateTime.Now;
                    NET_TIME time = new NET_TIME();
                    time.dwYear = current.Year;
                    time.dwMonth = current.Month;
                    time.dwDay = current.Day;
                    time.dwHour = current.Hour;
                    time.dwMinute = current.Minute;
                    time.dwSecond = current.Second;
                    DHClient.DHSetDevConfig(m_LoginID, time);
                }
            }
            catch { }
        }

        public override void FindFile(DateTime from, DateTime to)
        {
            return;
        }
        public override void StartPlayVideo(int index)
        {
            return;
        }
        public override void StopPlayVideo(int index)
        {
            return;
        }
        public override void PausePlayVideo(int index)
        {
            return;
        }
        public override void FowardPlayVideo(int index)
        {
            return;
        }
        public override void BackPlayVideo(int index)
        {
            return;
        }
    }
}
